package com.lijunhuayc.sample;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.AppCompatButton;
import android.util.AttributeSet;


public class DownloadButton extends AppCompatButton {

    private static final String TAG = "DownloadButton";

    private Paint mPaint;
    private Drawable mDrawable;
    private long mMax;
    private float mRatio;

    private boolean enableProgress =true;

    public DownloadButton(Context context) {
        this(context, null);
    }

    public DownloadButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        mPaint = new Paint();
        mPaint.setColor(Color.BLUE);

        mDrawable = new ColorDrawable(Color.BLUE);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (enableProgress) {
            float left = 0;
            float top = 0;
            float right = mRatio * getWidth();//进度的比值乘以按钮的宽度
            float bottom = getHeight();
            canvas.drawRect(left, top, right, bottom, mPaint);
        }
        //设置Drawable边界
//        mDrawable.setBounds((int)left, (int)top, (int)right, (int)bottom);
//        mDrawable.draw(canvas);//将drawable绘制到画布上
        //一定要调用super.onDraw，super方法里面实现了按钮的文本绘制
        super.onDraw(canvas);
    }

    /**
     * 下载进度，下载了多少个字节
     * @param progress 下载的字节数
     */
    public void setProgress(int progress) {
        //设置文本为百分数
        mRatio = progress * 1.0f / mMax;
        String percentString = String.valueOf((int)(mRatio * 100)) + "%";
        setText(percentString);//触发重新绘制，去绘制文本
    }

    public void setMax(long max) {
        mMax = max;
    }

    /**
     * 同步下载按钮的状态
     * @param appDetailData
     */
    /*public void syncState(AppDetailBean appDetailData) {
        //通过下载管理者，获取app详情对应的apk的下载相关的信息
        DownloadInfo downloadInfo = DownloadManager.getInstance().getDownloadInfo(getContext(),
                appDetailData.getPackageName(), appDetailData.getSize(), appDetailData.getDownloadUrl());
        Log.d(TAG, "syncState: " + downloadInfo.status);
        //将DownButton作为观察者加入DownloaManager观察者集合
        DownloadManager.getInstance().addObserver(downloadInfo.packageName, this);
        //拿到信息之后，就更新状态
        updateStatus(downloadInfo);
    }

    private void updateStatus(DownloadInfo downloadInfo) {
        switch (downloadInfo.status) {
            case DownloadManager.STATE_INSTALLED:
                setText("打开");
                break;
            case DownloadManager.STATE_DOWNLOADED:
                clearProgress();//清除矩形进度条
                setText("安装");
                break;
            case DownloadManager.STATE_UN_DOWNLOAD:
                setText("下载");
                break;
            case DownloadManager.STATE_WAITING:
                setText("等待");
                break;
            case DownloadManager.STATE_DOWNLOADING:
                //设置最大值
                enableProgress = true;
                setMax(downloadInfo.size);
                //再更新进度
                setProgress(downloadInfo.progress);
                break;
            case DownloadManager.STATE_PAUSE:
                setText("继续");
                break;
            case DownloadManager.STATE_FAILED:
                setText("重试");
                break;
        }
    }

    *//**
     * 当下载完成清除矩形进度条
     *//*
    private void clearProgress() {
        enableProgress = false;
        invalidate();
    }

    @Override
    public void update(Observable o, Object arg) {
        final DownloadInfo downloadInfo = (DownloadInfo) arg;
        post(new Runnable() {
            @Override
            public void run() {
                //在主线程更新ui
                updateStatus(downloadInfo);
            }
        });
    }*/
}
